package com.eling.elcms.travel.model;

import com.eling.elcms.basedoc.model.EnumSerializer;
import com.eling.elcms.core.dao.annotation.Between;
import com.eling.elcms.core.dao.annotation.CriteriaEnhancer;
import com.eling.elcms.core.dao.annotation.CriteriaEnhancers;
import com.eling.elcms.core.dao.annotation.In;
import com.eling.elcms.core.dao.hibernate.OrderCriteriaEnhancer;
import com.eling.elcms.core.dao.hibernate.search.EnumBridge;
import com.eling.elcms.core.model.BaseModel;
import com.eling.elcms.system.model.CommonUser;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import org.hibernate.search.annotations.*;

import javax.persistence.*;
import java.util.Date;
import java.util.Set;

/**
 * 组团预约
 * 
 * @author zhoulin
 *
 */
@Entity
@Table(name = "travel_groupreserve")
@Indexed
@CriteriaEnhancers(criteriaEnhancers = {
		@CriteriaEnhancer(impl = OrderCriteriaEnhancer.class, parameter = "checkInDate:desc") })
public class GroupReserve extends BaseModel {

	private static final long serialVersionUID = -6460828821817620110L;

	@Id
	@In
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@DocumentId
	private Long pkGroupReserve;

	/** 报名团 */
	@OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.MERGE)
	@JoinColumn(name = "pkPlanItemGroup")
	@IndexedEmbedded(depth = 1)
	private PlanItemGroup planItemGroup;

	/** 预约日期 */
	@Field
	@Column
	@Between
	private Date reserveDate;

	/** 入住日期 */
	@Field
	@Column
	@Between
	private Date checkInDate;

	/** 退房日期 */
	@Field
	@Column
	@Between
	private Date checkOutDate;

	/** 经办人 */
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "pkUser")
	@IndexedEmbedded(depth = 1)
	private CommonUser operator;

	/** 入住机构类型 */
	@Enumerated(EnumType.STRING)
	@Field
	@FieldBridge(impl = EnumBridge.class)
	@In
	private CheckInType checkInType;

	/** 联盟机构楼宇 */
	@Field
	@Column
	private String allianceBuilding;

	/** 联盟机构房间 */
	@Field
	@Column
	private String allianceRoom;

	/** 直属机构房间 */
	@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.MERGE)
	@IndexedEmbedded(depth = 1)
	@JoinTable(name = "travel_group_roomregister", joinColumns = @JoinColumn(name = "pkGroupReserve"), inverseJoinColumns = @JoinColumn(name = "pkRoomRegister"))
	private Set<RoomRegister> roomRegisters;

	/** 说明 */
	@Field
	@Column
	private String description;

	@Version
	private Integer version;

	@JsonSerialize(using = EnumSerializer.class)
	public static enum CheckInType {
		Alliance("联盟机构"), Organization("直属机构");

		private String display;

		CheckInType(String display) {
			this.display = display;
		}

		public String getDisplay() {
			return display;
		}
	}
	
	/**
	 * 计算房间数量
	 * @return
	 */
	public Integer getRoomNum(){
		return getRoomRegisters()!=null?getRoomRegisters().size():0;
	}
	
	public Long getPkGroupReserve() {
		return pkGroupReserve;
	}

	public void setPkGroupReserve(Long pkGroupReserve) {
		this.pkGroupReserve = pkGroupReserve;
	}

	public PlanItemGroup getPlanItemGroup() {
		return planItemGroup;
	}

	public void setPlanItemGroup(PlanItemGroup planItemGroup) {
		this.planItemGroup = planItemGroup;
	}

	public Date getReserveDate() {
		return reserveDate;
	}

	public void setReserveDate(Date reserveDate) {
		this.reserveDate = reserveDate;
	}

	public Date getCheckInDate() {
		return checkInDate;
	}

	public void setCheckInDate(Date checkInDate) {
		this.checkInDate = checkInDate;
	}

	public Date getCheckOutDate() {
		return checkOutDate;
	}

	public void setCheckOutDate(Date checkOutDate) {
		this.checkOutDate = checkOutDate;
	}

	public CommonUser getOperator() {
		return operator;
	}

	public void setOperator(CommonUser operator) {
		this.operator = operator;
	}

	public CheckInType getCheckInType() {
		return checkInType;
	}

	public void setCheckInType(CheckInType checkInType) {
		this.checkInType = checkInType;
	}

	public String getAllianceBuilding() {
		return allianceBuilding;
	}

	public void setAllianceBuilding(String allianceBuilding) {
		this.allianceBuilding = allianceBuilding;
	}

	public String getAllianceRoom() {
		return allianceRoom;
	}

	public void setAllianceRoom(String allianceRoom) {
		this.allianceRoom = allianceRoom;
	}

	public Set<RoomRegister> getRoomRegisters() {
		return roomRegisters;
	}

	public void setRoomRegisters(Set<RoomRegister> roomRegisters) {
		this.roomRegisters = roomRegisters;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Integer getVersion() {
		return version;
	}

	public void setVersion(Integer version) {
		this.version = version;
	}

}
